﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Reflection;
using StockholmsStad.Windows.UI.Infrastructure;
using StockholmsStad.Windows.UI.Models;

namespace StockholmsStad.Windows.UI
{
    /// <summary>
    /// Interaction logic for LoginDialog.xaml
    /// </summary>
    public partial class LogonScreen : INotifyPropertyChanged 
    {
        private Visibility hintVisibility;

        public LogonScreen()
        {
            Initialized += LogonScreen_Initialized;
            InitializeComponent();

            DataContext = this;
            HintVisibility = Visibility.Hidden;
            xUsername.Focus();
        }

        private void LogonScreen_Initialized(object sender, System.EventArgs e)
        {
            TextBoxVersion.Text = VersionHelper.AssemblyVersion;
        }

        /// <summary>
        /// Returns the username entered within the UI.
        /// </summary>
        public string UserName
        {
            get { return xUsername.Text; }
        }

        /// <summary>
        /// Returns the password entered within the UI.
        /// </summary>
        public string Password
        {
            get { return xPassword.Password; }
        }

        public bool HintVisible
        {
            get { return HintVisibility == Visibility.Visible; }
            set { HintVisibility = value ? Visibility.Visible : Visibility.Hidden; }
        }

        public Visibility HintVisibility
        {
            get { return hintVisibility; }
            set
            {
                if (value != hintVisibility)
                {
                    hintVisibility = value;
                    OnPropertyChanged("HintVisibility");
                }
            }
        }

        private void LogonButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void DoCredentialsFocussed(object sender, RoutedEventArgs e)
        {
            var tb = sender as TextBoxBase;
            if (tb == null)
            {
                var pwb = sender as PasswordBox;
                if (pwb != null) 
                    pwb.SelectAll();
            }
            else
            {
                tb.SelectAll();
            }
        }

        #region INotifyPropertyChanged Members

        private event PropertyChangedEventHandler propertyChangedEvent;

        public event PropertyChangedEventHandler PropertyChanged
        {
            add { propertyChangedEvent += value; }
            remove { propertyChangedEvent -= value; }
        }

        protected void OnPropertyChanged(string prop)
        {
            if (propertyChangedEvent != null)
                propertyChangedEvent(this, new PropertyChangedEventArgs(prop));
        }

        #endregion
    }
}
